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/uaccess.h> 8 #include <linux/printk.h> 9 #include <asm/nospec-branch.h> 10 #include <asm/abs_lowcore.h> 11 #include <asm/alternative.h> 12 #include <asm/facility.h> 13 #include <asm/sections.h> 14 #include <asm/machine.h> 15 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 24 unsigned long __bootdata_preserved(machine_features[1]); 25 26 struct 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 32 static struct alt_debug __bootdata_preserved(alt_debug); 33 34 static 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 50 void __apply_alternatives(struct alt_instr *start, struct alt_instr *end, unsigned int ctx) 51 { 52 struct alt_debug *d; 53 struct alt_instr *a; 54 bool debug, replace; 55 u8 *old, *new; 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 */ 61 d = &alt_debug; 62 for (a = start; a < end; a++) { 63 if (!(a->ctx & ctx)) 64 continue; 65 switch (a->type) { 66 case ALT_TYPE_FACILITY: 67 replace = test_facility(a->data); 68 debug = __test_facility(a->data, d->facilities); 69 break; 70 case ALT_TYPE_FEATURE: 71 replace = test_machine_feature(a->data); 72 debug = __test_machine_feature(a->data, d->mfeatures); 73 break; 74 case ALT_TYPE_SPEC: 75 replace = nobp_enabled(); 76 debug = d->spec; 77 break; 78 default: 79 replace = false; 80 debug = false; 81 } 82 if (!replace) 83 continue; 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); 89 } 90 } 91