xref: /linux-6.15/arch/mips/kernel/module.c (revision f6bec26c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Copyright (C) 2001 Rusty Russell.
5  *  Copyright (C) 2003, 2004 Ralf Baechle ([email protected])
6  *  Copyright (C) 2005 Thiemo Seufer
7  */
8 
9 #undef DEBUG
10 
11 #include <linux/extable.h>
12 #include <linux/moduleloader.h>
13 #include <linux/elf.h>
14 #include <linux/mm.h>
15 #include <linux/numa.h>
16 #include <linux/vmalloc.h>
17 #include <linux/slab.h>
18 #include <linux/fs.h>
19 #include <linux/string.h>
20 #include <linux/kernel.h>
21 #include <linux/spinlock.h>
22 #include <linux/jump_label.h>
23 #include <linux/execmem.h>
24 #include <asm/jump_label.h>
25 
26 struct mips_hi16 {
27 	struct mips_hi16 *next;
28 	Elf_Addr *addr;
29 	Elf_Addr value;
30 };
31 
32 static LIST_HEAD(dbe_list);
33 static DEFINE_SPINLOCK(dbe_lock);
34 
35 #ifdef MODULES_VADDR
36 static struct execmem_info execmem_info __ro_after_init;
37 
38 struct execmem_info __init *execmem_arch_setup(void)
39 {
40 	execmem_info = (struct execmem_info){
41 		.ranges = {
42 			[EXECMEM_DEFAULT] = {
43 				.start	= MODULES_VADDR,
44 				.end	= MODULES_END,
45 				.pgprot	= PAGE_KERNEL,
46 				.alignment = 1,
47 			},
48 		},
49 	};
50 
51 	return &execmem_info;
52 }
53 #endif
54 
55 static void apply_r_mips_32(u32 *location, u32 base, Elf_Addr v)
56 {
57 	*location = base + v;
58 }
59 
60 static int apply_r_mips_26(struct module *me, u32 *location, u32 base,
61 			   Elf_Addr v)
62 {
63 	if (v % 4) {
64 		pr_err("module %s: dangerous R_MIPS_26 relocation\n",
65 		       me->name);
66 		return -ENOEXEC;
67 	}
68 
69 	if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
70 		pr_err("module %s: relocation overflow\n",
71 		       me->name);
72 		return -ENOEXEC;
73 	}
74 
75 	*location = (*location & ~0x03ffffff) |
76 		    ((base + (v >> 2)) & 0x03ffffff);
77 
78 	return 0;
79 }
80 
81 static int apply_r_mips_hi16(struct module *me, u32 *location, Elf_Addr v,
82 			     bool rela)
83 {
84 	struct mips_hi16 *n;
85 
86 	if (rela) {
87 		*location = (*location & 0xffff0000) |
88 			    ((((long long) v + 0x8000LL) >> 16) & 0xffff);
89 		return 0;
90 	}
91 
92 	/*
93 	 * We cannot relocate this one now because we don't know the value of
94 	 * the carry we need to add.  Save the information, and let LO16 do the
95 	 * actual relocation.
96 	 */
97 	n = kmalloc(sizeof *n, GFP_KERNEL);
98 	if (!n)
99 		return -ENOMEM;
100 
101 	n->addr = (Elf_Addr *)location;
102 	n->value = v;
103 	n->next = me->arch.r_mips_hi16_list;
104 	me->arch.r_mips_hi16_list = n;
105 
106 	return 0;
107 }
108 
109 static void free_relocation_chain(struct mips_hi16 *l)
110 {
111 	struct mips_hi16 *next;
112 
113 	while (l) {
114 		next = l->next;
115 		kfree(l);
116 		l = next;
117 	}
118 }
119 
120 static int apply_r_mips_lo16(struct module *me, u32 *location,
121 			     u32 base, Elf_Addr v, bool rela)
122 {
123 	unsigned long insnlo = base;
124 	struct mips_hi16 *l;
125 	Elf_Addr val, vallo;
126 
127 	if (rela) {
128 		*location = (*location & 0xffff0000) | (v & 0xffff);
129 		return 0;
130 	}
131 
132 	/* Sign extend the addend we extract from the lo insn.	*/
133 	vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
134 
135 	if (me->arch.r_mips_hi16_list != NULL) {
136 		l = me->arch.r_mips_hi16_list;
137 		while (l != NULL) {
138 			struct mips_hi16 *next;
139 			unsigned long insn;
140 
141 			/*
142 			 * The value for the HI16 had best be the same.
143 			 */
144 			if (v != l->value)
145 				goto out_danger;
146 
147 			/*
148 			 * Do the HI16 relocation.  Note that we actually don't
149 			 * need to know anything about the LO16 itself, except
150 			 * where to find the low 16 bits of the addend needed
151 			 * by the LO16.
152 			 */
153 			insn = *l->addr;
154 			val = ((insn & 0xffff) << 16) + vallo;
155 			val += v;
156 
157 			/*
158 			 * Account for the sign extension that will happen in
159 			 * the low bits.
160 			 */
161 			val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
162 
163 			insn = (insn & ~0xffff) | val;
164 			*l->addr = insn;
165 
166 			next = l->next;
167 			kfree(l);
168 			l = next;
169 		}
170 
171 		me->arch.r_mips_hi16_list = NULL;
172 	}
173 
174 	/*
175 	 * Ok, we're done with the HI16 relocs.	 Now deal with the LO16.
176 	 */
177 	val = v + vallo;
178 	insnlo = (insnlo & ~0xffff) | (val & 0xffff);
179 	*location = insnlo;
180 
181 	return 0;
182 
183 out_danger:
184 	free_relocation_chain(l);
185 	me->arch.r_mips_hi16_list = NULL;
186 
187 	pr_err("module %s: dangerous R_MIPS_LO16 relocation\n", me->name);
188 
189 	return -ENOEXEC;
190 }
191 
192 static int apply_r_mips_pc(struct module *me, u32 *location, u32 base,
193 			   Elf_Addr v, unsigned int bits)
194 {
195 	unsigned long mask = GENMASK(bits - 1, 0);
196 	unsigned long se_bits;
197 	long offset;
198 
199 	if (v % 4) {
200 		pr_err("module %s: dangerous R_MIPS_PC%u relocation\n",
201 		       me->name, bits);
202 		return -ENOEXEC;
203 	}
204 
205 	/* retrieve & sign extend implicit addend if any */
206 	offset = base & mask;
207 	offset |= (offset & BIT(bits - 1)) ? ~mask : 0;
208 
209 	offset += ((long)v - (long)location) >> 2;
210 
211 	/* check the sign bit onwards are identical - ie. we didn't overflow */
212 	se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0;
213 	if ((offset & ~mask) != (se_bits & ~mask)) {
214 		pr_err("module %s: relocation overflow\n", me->name);
215 		return -ENOEXEC;
216 	}
217 
218 	*location = (*location & ~mask) | (offset & mask);
219 
220 	return 0;
221 }
222 
223 static int apply_r_mips_pc16(struct module *me, u32 *location, u32 base,
224 			     Elf_Addr v)
225 {
226 	return apply_r_mips_pc(me, location, base, v, 16);
227 }
228 
229 static int apply_r_mips_pc21(struct module *me, u32 *location, u32 base,
230 			     Elf_Addr v)
231 {
232 	return apply_r_mips_pc(me, location, base, v, 21);
233 }
234 
235 static int apply_r_mips_pc26(struct module *me, u32 *location, u32 base,
236 			     Elf_Addr v)
237 {
238 	return apply_r_mips_pc(me, location, base, v, 26);
239 }
240 
241 static int apply_r_mips_64(u32 *location, Elf_Addr v, bool rela)
242 {
243 	if (WARN_ON(!rela))
244 		return -EINVAL;
245 
246 	*(Elf_Addr *)location = v;
247 
248 	return 0;
249 }
250 
251 static int apply_r_mips_higher(u32 *location, Elf_Addr v, bool rela)
252 {
253 	if (WARN_ON(!rela))
254 		return -EINVAL;
255 
256 	*location = (*location & 0xffff0000) |
257 		    ((((long long)v + 0x80008000LL) >> 32) & 0xffff);
258 
259 	return 0;
260 }
261 
262 static int apply_r_mips_highest(u32 *location, Elf_Addr v, bool rela)
263 {
264 	if (WARN_ON(!rela))
265 		return -EINVAL;
266 
267 	*location = (*location & 0xffff0000) |
268 		    ((((long long)v + 0x800080008000LL) >> 48) & 0xffff);
269 
270 	return 0;
271 }
272 
273 /**
274  * reloc_handler() - Apply a particular relocation to a module
275  * @type: type of the relocation to apply
276  * @me: the module to apply the reloc to
277  * @location: the address at which the reloc is to be applied
278  * @base: the existing value at location for REL-style; 0 for RELA-style
279  * @v: the value of the reloc, with addend for RELA-style
280  * @rela: indication of is this a RELA (true) or REL (false) relocation
281  *
282  * Each implemented relocation function applies a particular type of
283  * relocation to the module @me. Relocs that may be found in either REL or RELA
284  * variants can be handled by making use of the @base & @v parameters which are
285  * set to values which abstract the difference away from the particular reloc
286  * implementations.
287  *
288  * Return: 0 upon success, else -ERRNO
289  */
290 static int reloc_handler(u32 type, struct module *me, u32 *location, u32 base,
291 			 Elf_Addr v, bool rela)
292 {
293 	switch (type) {
294 	case R_MIPS_NONE:
295 		break;
296 	case R_MIPS_32:
297 		apply_r_mips_32(location, base, v);
298 		break;
299 	case R_MIPS_26:
300 		return apply_r_mips_26(me, location, base, v);
301 	case R_MIPS_HI16:
302 		return apply_r_mips_hi16(me, location, v, rela);
303 	case R_MIPS_LO16:
304 		return apply_r_mips_lo16(me, location, base, v, rela);
305 	case R_MIPS_PC16:
306 		return apply_r_mips_pc16(me, location, base, v);
307 	case R_MIPS_PC21_S2:
308 		return apply_r_mips_pc21(me, location, base, v);
309 	case R_MIPS_PC26_S2:
310 		return apply_r_mips_pc26(me, location, base, v);
311 	case R_MIPS_64:
312 		return apply_r_mips_64(location, v, rela);
313 	case R_MIPS_HIGHER:
314 		return apply_r_mips_higher(location, v, rela);
315 	case R_MIPS_HIGHEST:
316 		return apply_r_mips_highest(location, v, rela);
317 	default:
318 		pr_err("%s: Unknown relocation type %u\n", me->name, type);
319 		return -EINVAL;
320 	}
321 
322 	return 0;
323 }
324 
325 static int __apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
326 			    unsigned int symindex, unsigned int relsec,
327 			    struct module *me, bool rela)
328 {
329 	union {
330 		Elf_Mips_Rel *rel;
331 		Elf_Mips_Rela *rela;
332 	} r;
333 	Elf_Sym *sym;
334 	u32 *location, base;
335 	unsigned int i, type;
336 	Elf_Addr v;
337 	int err = 0;
338 	size_t reloc_sz;
339 
340 	pr_debug("Applying relocate section %u to %u\n", relsec,
341 	       sechdrs[relsec].sh_info);
342 
343 	r.rel = (void *)sechdrs[relsec].sh_addr;
344 	reloc_sz = rela ? sizeof(*r.rela) : sizeof(*r.rel);
345 	me->arch.r_mips_hi16_list = NULL;
346 	for (i = 0; i < sechdrs[relsec].sh_size / reloc_sz; i++) {
347 		/* This is where to make the change */
348 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
349 			+ r.rel->r_offset;
350 		/* This is the symbol it is referring to */
351 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
352 			+ ELF_MIPS_R_SYM(*r.rel);
353 		if (sym->st_value >= -MAX_ERRNO) {
354 			/* Ignore unresolved weak symbol */
355 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
356 				continue;
357 			pr_warn("%s: Unknown symbol %s\n",
358 				me->name, strtab + sym->st_name);
359 			err = -ENOENT;
360 			goto out;
361 		}
362 
363 		type = ELF_MIPS_R_TYPE(*r.rel);
364 
365 		if (rela) {
366 			v = sym->st_value + r.rela->r_addend;
367 			base = 0;
368 			r.rela = &r.rela[1];
369 		} else {
370 			v = sym->st_value;
371 			base = *location;
372 			r.rel = &r.rel[1];
373 		}
374 
375 		err = reloc_handler(type, me, location, base, v, rela);
376 		if (err)
377 			goto out;
378 	}
379 
380 out:
381 	/*
382 	 * Normally the hi16 list should be deallocated at this point. A
383 	 * malformed binary however could contain a series of R_MIPS_HI16
384 	 * relocations not followed by a R_MIPS_LO16 relocation, or if we hit
385 	 * an error processing a reloc we might have gotten here before
386 	 * reaching the R_MIPS_LO16. In either case, free up the list and
387 	 * return an error.
388 	 */
389 	if (me->arch.r_mips_hi16_list) {
390 		free_relocation_chain(me->arch.r_mips_hi16_list);
391 		me->arch.r_mips_hi16_list = NULL;
392 		err = err ?: -ENOEXEC;
393 	}
394 
395 	return err;
396 }
397 
398 int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
399 		   unsigned int symindex, unsigned int relsec,
400 		   struct module *me)
401 {
402 	return __apply_relocate(sechdrs, strtab, symindex, relsec, me, false);
403 }
404 
405 #ifdef CONFIG_MODULES_USE_ELF_RELA
406 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
407 		       unsigned int symindex, unsigned int relsec,
408 		       struct module *me)
409 {
410 	return __apply_relocate(sechdrs, strtab, symindex, relsec, me, true);
411 }
412 #endif /* CONFIG_MODULES_USE_ELF_RELA */
413 
414 /* Given an address, look for it in the module exception tables. */
415 const struct exception_table_entry *search_module_dbetables(unsigned long addr)
416 {
417 	unsigned long flags;
418 	const struct exception_table_entry *e = NULL;
419 	struct mod_arch_specific *dbe;
420 
421 	spin_lock_irqsave(&dbe_lock, flags);
422 	list_for_each_entry(dbe, &dbe_list, dbe_list) {
423 		e = search_extable(dbe->dbe_start,
424 				   dbe->dbe_end - dbe->dbe_start, addr);
425 		if (e)
426 			break;
427 	}
428 	spin_unlock_irqrestore(&dbe_lock, flags);
429 
430 	/* Now, if we found one, we are running inside it now, hence
431 	   we cannot unload the module, hence no refcnt needed. */
432 	return e;
433 }
434 
435 /* Put in dbe list if necessary. */
436 int module_finalize(const Elf_Ehdr *hdr,
437 		    const Elf_Shdr *sechdrs,
438 		    struct module *me)
439 {
440 	const Elf_Shdr *s;
441 	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
442 
443 	if (IS_ENABLED(CONFIG_JUMP_LABEL))
444 		jump_label_apply_nops(me);
445 
446 	INIT_LIST_HEAD(&me->arch.dbe_list);
447 	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
448 		if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
449 			continue;
450 		me->arch.dbe_start = (void *)s->sh_addr;
451 		me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
452 		spin_lock_irq(&dbe_lock);
453 		list_add(&me->arch.dbe_list, &dbe_list);
454 		spin_unlock_irq(&dbe_lock);
455 	}
456 	return 0;
457 }
458 
459 void module_arch_cleanup(struct module *mod)
460 {
461 	spin_lock_irq(&dbe_lock);
462 	list_del(&mod->arch.dbe_list);
463 	spin_unlock_irq(&dbe_lock);
464 }
465