xref: /linux/arch/s390/kernel/alternative.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #ifndef pr_fmt
4 #define pr_fmt(fmt)	"alt: " fmt
5 #endif
6 
7 #include <linux/hex.h>
8 #include <linux/uaccess.h>
9 #include <linux/printk.h>
10 #include <asm/nospec-branch.h>
11 #include <asm/abs_lowcore.h>
12 #include <asm/alternative.h>
13 #include <asm/facility.h>
14 #include <asm/sections.h>
15 #include <asm/machine.h>
16 
17 #ifndef a_debug
18 #define a_debug		pr_debug
19 #endif
20 
21 #ifndef __kernel_va
22 #define __kernel_va(x)	(void *)(x)
23 #endif
24 
25 unsigned long __bootdata_preserved(machine_features[1]);
26 
27 struct alt_debug {
28 	unsigned long facilities[MAX_FACILITY_BIT / BITS_PER_LONG];
29 	unsigned long mfeatures[MAX_MFEATURE_BIT / BITS_PER_LONG];
30 	int spec;
31 };
32 
33 static struct alt_debug __bootdata_preserved(alt_debug);
34 
35 static void alternative_dump(u8 *old, u8 *new, unsigned int len, unsigned int type, unsigned int data)
36 {
37 	char oinsn[33], ninsn[33];
38 	unsigned long kptr;
39 	unsigned int pos;
40 
41 	for (pos = 0; pos < len && 2 * pos < sizeof(oinsn) - 3; pos++)
42 		hex_byte_pack(&oinsn[2 * pos], old[pos]);
43 	oinsn[2 * pos] = 0;
44 	for (pos = 0; pos < len && 2 * pos < sizeof(ninsn) - 3; pos++)
45 		hex_byte_pack(&ninsn[2 * pos], new[pos]);
46 	ninsn[2 * pos] = 0;
47 	kptr = (unsigned long)__kernel_va(old);
48 	a_debug("[%d/%3d] %016lx: %s -> %s\n", type, data, kptr, oinsn, ninsn);
49 }
50 
51 void __apply_alternatives(struct alt_instr *start, struct alt_instr *end, unsigned int ctx)
52 {
53 	struct alt_debug *d;
54 	struct alt_instr *a;
55 	bool debug, replace;
56 	u8 *old, *new;
57 
58 	/*
59 	 * The scan order should be from start to end. A later scanned
60 	 * alternative code can overwrite previously scanned alternative code.
61 	 */
62 	d = &alt_debug;
63 	for (a = start; a < end; a++) {
64 		if (!(a->ctx & ctx))
65 			continue;
66 		switch (a->type) {
67 		case ALT_TYPE_FACILITY:
68 			replace = test_facility(a->data);
69 			debug = __test_facility(a->data, d->facilities);
70 			break;
71 		case ALT_TYPE_FEATURE:
72 			replace = test_machine_feature(a->data);
73 			debug = __test_machine_feature(a->data, d->mfeatures);
74 			break;
75 		case ALT_TYPE_SPEC:
76 			replace = nobp_enabled();
77 			debug = d->spec;
78 			break;
79 		default:
80 			replace = false;
81 			debug = false;
82 		}
83 		if (!replace)
84 			continue;
85 		old = (u8 *)&a->instr_offset + a->instr_offset;
86 		new = (u8 *)&a->repl_offset + a->repl_offset;
87 		if (debug)
88 			alternative_dump(old, new, a->instrlen, a->type, a->data);
89 		s390_kernel_write(old, new, a->instrlen);
90 	}
91 }
92