xref: /openbmc/linux/arch/parisc/kernel/alternative.c (revision 3381df09)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Alternative live-patching for parisc.
4  *    Copyright (C) 2018 Helge Deller <deller@gmx.de>
5  *
6  */
7 
8 #include <asm/processor.h>
9 #include <asm/sections.h>
10 #include <asm/alternative.h>
11 
12 #include <linux/module.h>
13 
14 static int no_alternatives;
15 static int __init setup_no_alternatives(char *str)
16 {
17 	no_alternatives = 1;
18 	return 1;
19 }
20 __setup("no-alternatives", setup_no_alternatives);
21 
22 void __init_or_module apply_alternatives(struct alt_instr *start,
23 		 struct alt_instr *end, const char *module_name)
24 {
25 	struct alt_instr *entry;
26 	int index = 0, applied = 0;
27 	int num_cpus = num_online_cpus();
28 	u32 cond_check;
29 
30 	cond_check = ALT_COND_ALWAYS |
31 		((num_cpus == 1) ? ALT_COND_NO_SMP : 0) |
32 		((cache_info.dc_size == 0) ? ALT_COND_NO_DCACHE : 0) |
33 		((cache_info.ic_size == 0) ? ALT_COND_NO_ICACHE : 0) |
34 		(running_on_qemu ? ALT_COND_RUN_ON_QEMU : 0) |
35 		((split_tlb == 0) ? ALT_COND_NO_SPLIT_TLB : 0) |
36 		/*
37 		 * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit
38 		 * set (bit #61, big endian), we have to flush and sync every
39 		 * time IO-PDIR is changed in Ike/Astro.
40 		 */
41 		(((boot_cpu_data.cpu_type > pcxw_) &&
42 		  ((boot_cpu_data.pdc.capabilities & PDC_MODEL_IOPDIR_FDC) == 0))
43 			? ALT_COND_NO_IOC_FDC : 0);
44 
45 	for (entry = start; entry < end; entry++, index++) {
46 
47 		u32 *from, cond, replacement;
48 		s32 len;
49 
50 		from = (u32 *)((ulong)&entry->orig_offset + entry->orig_offset);
51 		len = entry->len;
52 		cond = entry->cond;
53 		replacement = entry->replacement;
54 
55 		WARN_ON(!cond);
56 
57 		if ((cond & ALT_COND_ALWAYS) == 0 && no_alternatives)
58 			continue;
59 
60 		pr_debug("Check %d: Cond 0x%x, Replace %02d instructions @ 0x%px with 0x%08x\n",
61 			index, cond, len, from, replacement);
62 
63 		/* Bounce out if none of the conditions are true. */
64 		if ((cond & cond_check) == 0)
65 			continue;
66 
67 		/* Want to replace pdtlb by a pdtlb,l instruction? */
68 		if (replacement == INSN_PxTLB) {
69 			replacement = *from;
70 			if (boot_cpu_data.cpu_type >= pcxu) /* >= pa2.0 ? */
71 				replacement |= (1 << 10); /* set el bit */
72 		}
73 
74 		/*
75 		 * Replace instruction with NOPs?
76 		 * For long distance insert a branch instruction instead.
77 		 */
78 		if (replacement == INSN_NOP && len > 1)
79 			replacement = 0xe8000002 + (len-2)*8; /* "b,n .+8" */
80 
81 		pr_debug("ALTERNATIVE %3d: Cond %2x, Replace %2d instructions to 0x%08x @ 0x%px (%pS)\n",
82 			index, cond, len, replacement, from, from);
83 
84 		if (len < 0) {
85 			/* Replace multiple instruction by new code */
86 			u32 *source;
87 			len = -len;
88 			source = (u32 *)((ulong)&entry->replacement + entry->replacement);
89 			memcpy(from, source, 4 * len);
90 		} else {
91 			/* Replace by one instruction */
92 			*from = replacement;
93 		}
94 		applied++;
95 	}
96 
97 	pr_info("%s%salternatives: applied %d out of %d patches\n",
98 		module_name ? : "", module_name ? " " : "",
99 		applied, index);
100 }
101 
102 
103 void __init apply_alternatives_all(void)
104 {
105 	set_kernel_text_rw(1);
106 
107 	apply_alternatives((struct alt_instr *) &__alt_instructions,
108 		(struct alt_instr *) &__alt_instructions_end, NULL);
109 
110 	set_kernel_text_rw(0);
111 }
112