xref: /linux/arch/riscv/kernel/alternative.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * alternative runtime patching
4  * inspired by the ARM64 and x86 version
5  *
6  * Copyright (C) 2021 Sifive.
7  */
8 
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/cpu.h>
12 #include <linux/uaccess.h>
13 #include <asm/alternative.h>
14 #include <asm/module.h>
15 #include <asm/sections.h>
16 #include <asm/vdso.h>
17 #include <asm/vendorid_list.h>
18 #include <asm/sbi.h>
19 #include <asm/csr.h>
20 #include <asm/insn.h>
21 #include <asm/text-patching.h>
22 
23 struct cpu_manufacturer_info_t {
24 	unsigned long vendor_id;
25 	unsigned long arch_id;
26 	unsigned long imp_id;
27 	void (*patch_func)(struct alt_entry *begin, struct alt_entry *end,
28 				  unsigned long archid, unsigned long impid,
29 				  unsigned int stage);
30 };
31 
32 static void riscv_fill_cpu_mfr_info(struct cpu_manufacturer_info_t *cpu_mfr_info)
33 {
34 #ifdef CONFIG_RISCV_M_MODE
35 	cpu_mfr_info->vendor_id = csr_read(CSR_MVENDORID);
36 	cpu_mfr_info->arch_id = csr_read(CSR_MARCHID);
37 	cpu_mfr_info->imp_id = csr_read(CSR_MIMPID);
38 #else
39 	cpu_mfr_info->vendor_id = sbi_get_mvendorid();
40 	cpu_mfr_info->arch_id = sbi_get_marchid();
41 	cpu_mfr_info->imp_id = sbi_get_mimpid();
42 #endif
43 
44 	switch (cpu_mfr_info->vendor_id) {
45 #ifdef CONFIG_ERRATA_ANDES
46 	case ANDES_VENDOR_ID:
47 		cpu_mfr_info->patch_func = andes_errata_patch_func;
48 		break;
49 #endif
50 #ifdef CONFIG_ERRATA_MIPS
51 	case MIPS_VENDOR_ID:
52 		cpu_mfr_info->patch_func = mips_errata_patch_func;
53 		break;
54 #endif
55 #ifdef CONFIG_ERRATA_SIFIVE
56 	case SIFIVE_VENDOR_ID:
57 		cpu_mfr_info->patch_func = sifive_errata_patch_func;
58 		break;
59 #endif
60 #ifdef CONFIG_ERRATA_THEAD
61 	case THEAD_VENDOR_ID:
62 		cpu_mfr_info->patch_func = thead_errata_patch_func;
63 		break;
64 #endif
65 	default:
66 		cpu_mfr_info->patch_func = NULL;
67 	}
68 }
69 
70 static u32 riscv_instruction_at(void *p)
71 {
72 	u16 *parcel = p;
73 
74 	return (u32)parcel[0] | (u32)parcel[1] << 16;
75 }
76 
77 static void riscv_alternative_fix_auipc_jalr(void *ptr, u32 auipc_insn,
78 					     u32 jalr_insn, int patch_offset,
79 					     bool early)
80 {
81 	u32 call[2] = { auipc_insn, jalr_insn };
82 	s32 imm;
83 
84 	/* get and adjust new target address */
85 	imm = riscv_insn_extract_utype_itype_imm(auipc_insn, jalr_insn);
86 	imm -= patch_offset;
87 
88 	/* update instructions */
89 	riscv_insn_insert_utype_itype_imm(&call[0], &call[1], imm);
90 
91 	/* patch the call place again */
92 	if (early) {
93 		memcpy(ptr, call, sizeof(call));
94 	} else {
95 		patch_text_nosync(ptr, call, sizeof(call));
96 	}
97 }
98 
99 static void riscv_alternative_fix_jal(void *ptr, u32 jal_insn, int patch_offset,
100 				      bool early)
101 {
102 	s32 imm;
103 
104 	/* get and adjust new target address */
105 	imm = riscv_insn_extract_jtype_imm(jal_insn);
106 	imm -= patch_offset;
107 
108 	/* update instruction */
109 	riscv_insn_insert_jtype_imm(&jal_insn, imm);
110 
111 	/* patch the call place again */
112 	if (early)
113 		memcpy(ptr, &jal_insn, sizeof(u32));
114 	else
115 		patch_text_nosync(ptr, &jal_insn, sizeof(u32));
116 }
117 
118 void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,
119 				   int patch_offset, bool early)
120 {
121 	int num_insn = len / sizeof(u32);
122 	int i;
123 
124 	for (i = 0; i < num_insn; i++) {
125 		u32 insn = riscv_instruction_at(alt_ptr + i * sizeof(u32));
126 
127 		/*
128 		 * May be the start of an auipc + jalr pair
129 		 * Needs to check that at least one more instruction
130 		 * is in the list.
131 		 */
132 		if (riscv_insn_is_auipc(insn) && i < num_insn - 1) {
133 			u32 insn2 = riscv_instruction_at(alt_ptr + (i + 1) * sizeof(u32));
134 
135 			if (!riscv_insn_is_jalr(insn2))
136 				continue;
137 
138 			/* if instruction pair is a call, it will use the ra register */
139 			if (RV_EXTRACT_RD_REG(insn) != 1)
140 				continue;
141 
142 			riscv_alternative_fix_auipc_jalr(alt_ptr + i * sizeof(u32),
143 							 insn, insn2, patch_offset,
144 							 early);
145 			i++;
146 		}
147 
148 		if (riscv_insn_is_jal(insn)) {
149 			s32 imm = riscv_insn_extract_jtype_imm(insn);
150 
151 			/* Don't modify jumps inside the alternative block */
152 			if ((alt_ptr + i * sizeof(u32) + imm) >= alt_ptr &&
153 			    (alt_ptr + i * sizeof(u32) + imm) < (alt_ptr + len))
154 				continue;
155 
156 			riscv_alternative_fix_jal(alt_ptr + i * sizeof(u32),
157 						  insn, patch_offset, early);
158 		}
159 	}
160 }
161 
162 /*
163  * This is called very early in the boot process (directly after we run
164  * a feature detect on the boot CPU). No need to worry about other CPUs
165  * here.
166  */
167 static void __init_or_module _apply_alternatives(struct alt_entry *begin,
168 						 struct alt_entry *end,
169 						 unsigned int stage)
170 {
171 	struct cpu_manufacturer_info_t cpu_mfr_info;
172 
173 	riscv_fill_cpu_mfr_info(&cpu_mfr_info);
174 
175 	riscv_cpufeature_patch_func(begin, end, stage);
176 
177 	if (!cpu_mfr_info.patch_func)
178 		return;
179 
180 	cpu_mfr_info.patch_func(begin, end,
181 				cpu_mfr_info.arch_id,
182 				cpu_mfr_info.imp_id,
183 				stage);
184 }
185 
186 static __always_inline
187 void __init apply_vdso_alternatives(void *base, size_t alternatives_begin, size_t alternatives_end)
188 {
189 	if (alternatives_begin == alternatives_end)
190 		return;
191 
192 	_apply_alternatives(base + alternatives_begin,
193 			    base + alternatives_end,
194 			    RISCV_ALTERNATIVES_BOOT);
195 }
196 
197 void __init apply_boot_alternatives(void)
198 {
199 	/* If called on non-boot cpu things could go wrong */
200 	WARN_ON(smp_processor_id() != 0);
201 
202 	_apply_alternatives((struct alt_entry *)__alt_start,
203 			    (struct alt_entry *)__alt_end,
204 			    RISCV_ALTERNATIVES_BOOT);
205 
206 	if (IS_ENABLED(CONFIG_MMU))
207 		apply_vdso_alternatives(vdso_start,
208 					__vdso_alternatives_start_offset,
209 					__vdso_alternatives_end_offset);
210 
211 	if (IS_ENABLED(CONFIG_RISCV_USER_CFI))
212 		apply_vdso_alternatives(vdso_cfi_start,
213 					__vdso_alternatives_start_cfi_offset,
214 					__vdso_alternatives_end_cfi_offset);
215 
216 	if (IS_ENABLED(CONFIG_COMPAT))
217 		apply_vdso_alternatives(compat_vdso_start,
218 					compat__vdso_alternatives_start_offset,
219 					compat__vdso_alternatives_end_offset);
220 }
221 
222 /*
223  * apply_early_boot_alternatives() is called from setup_vm() with MMU-off.
224  *
225  * Following requirements should be honoured for it to work correctly:
226  * 1) It should use PC-relative addressing for accessing kernel symbols.
227  *    To achieve this we always use GCC cmodel=medany.
228  * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
229  *    so disable compiler instrumentation when FTRACE is enabled.
230  *
231  * Currently, the above requirements are honoured by using custom CFLAGS
232  * for alternative.o in kernel/Makefile.
233  */
234 void __init apply_early_boot_alternatives(void)
235 {
236 #ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
237 	_apply_alternatives((struct alt_entry *)__alt_start,
238 			    (struct alt_entry *)__alt_end,
239 			    RISCV_ALTERNATIVES_EARLY_BOOT);
240 #endif
241 }
242 
243 #ifdef CONFIG_MODULES
244 void apply_module_alternatives(void *start, size_t length)
245 {
246 	_apply_alternatives((struct alt_entry *)start,
247 			    (struct alt_entry *)(start + length),
248 			    RISCV_ALTERNATIVES_MODULE);
249 }
250 #endif
251