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