blob: 09ba5f585634a8e223e9da74d51c85894f3bac1c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 *
4 * Copyright (C) 2001 Rusty Russell.
5 * Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
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
24#include <asm/pgtable.h> /* MODULE_START */
25
26struct mips_hi16 {
27 struct mips_hi16 *next;
28 Elf_Addr *addr;
29 Elf_Addr value;
30};
31
32static LIST_HEAD(dbe_list);
33static DEFINE_SPINLOCK(dbe_lock);
34
35/*
36 * Get the potential max trampolines size required of the init and
37 * non-init sections. Only used if we cannot find enough contiguous
38 * physically mapped memory to put the module into.
39 */
40static unsigned int
41get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
42 const char *secstrings, unsigned int symindex, bool is_init)
43{
44 unsigned long ret = 0;
45 unsigned int i, j;
46 Elf_Sym *syms;
47
48 /* Everything marked ALLOC (this includes the exported symbols) */
49 for (i = 1; i < hdr->e_shnum; ++i) {
50 unsigned int info = sechdrs[i].sh_info;
51
52 if (sechdrs[i].sh_type != SHT_REL
53 && sechdrs[i].sh_type != SHT_RELA)
54 continue;
55
56 /* Not a valid relocation section? */
57 if (info >= hdr->e_shnum)
58 continue;
59
60 /* Don't bother with non-allocated sections */
61 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
62 continue;
63
64 /* If it's called *.init*, and we're not init, we're
65 not interested */
66 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
67 != is_init)
68 continue;
69
70 syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
71 if (sechdrs[i].sh_type == SHT_REL) {
72 Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
73 unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
74
75 for (j = 0; j < size; ++j) {
76 Elf_Sym *sym;
77
78 if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
79 continue;
80
81 sym = syms + ELF_MIPS_R_SYM(rel[j]);
82 if (!is_init && sym->st_shndx != SHN_UNDEF)
83 continue;
84
85 ret += 4 * sizeof(int);
86 }
87 } else {
88 Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
89 unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
90
91 for (j = 0; j < size; ++j) {
92 Elf_Sym *sym;
93
94 if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
95 continue;
96
97 sym = syms + ELF_MIPS_R_SYM(rela[j]);
98 if (!is_init && sym->st_shndx != SHN_UNDEF)
99 continue;
100
101 ret += 4 * sizeof(int);
102 }
103 }
104 }
105
106 return ret;
107}
108
109#ifndef MODULE_START
110static void *alloc_phys(unsigned long size)
111{
112 unsigned order;
113 struct page *page;
114 struct page *p;
115
116 size = PAGE_ALIGN(size);
117 order = get_order(size);
118
119 page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
120 __GFP_THISNODE, order);
121 if (!page)
122 return NULL;
123
124 split_page(page, order);
125
126 /* mark all pages except for the last one */
127 for (p = page; p + 1 < page + (size >> PAGE_SHIFT); ++p)
128 set_bit(PG_owner_priv_1, &p->flags);
129
130 for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
131 __free_page(p);
132
133 return page_address(page);
134}
135#endif
136
137static void free_phys(void *ptr)
138{
139 struct page *page;
140 bool free;
141
142 page = virt_to_page(ptr);
143 do {
144 free = test_and_clear_bit(PG_owner_priv_1, &page->flags);
145 __free_page(page);
146 page++;
147 } while (free);
148}
149
150
151void *module_alloc(unsigned long size)
152{
153#ifdef MODULE_START
154 return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
155 GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE,
156 __builtin_return_address(0));
157#else
158 void *ptr;
159
160 if (size == 0)
161 return NULL;
162
163 ptr = alloc_phys(size);
164
165 /* If we failed to allocate physically contiguous memory,
166 * fall back to regular vmalloc. The module loader code will
167 * create jump tables to handle long jumps */
168 if (!ptr)
169 return vmalloc(size);
170
171 return ptr;
172#endif
173}
174
175static inline bool is_phys_addr(void *ptr)
176{
177#ifdef CONFIG_64BIT
178 return (KSEGX((unsigned long)ptr) == CKSEG0);
179#else
180 return (KSEGX(ptr) == KSEG0);
181#endif
182}
183
184/* Free memory returned from module_alloc */
185void module_memfree(void *module_region)
186{
187 if (is_phys_addr(module_region))
188 free_phys(module_region);
189 else
190 vfree(module_region);
191}
192
193static void *__module_alloc(int size, bool phys)
194{
195 void *ptr;
196
197 if (phys)
198 ptr = kmalloc(size, GFP_KERNEL);
199 else
200 ptr = vmalloc(size);
201 return ptr;
202}
203
204static void __module_free(void *ptr)
205{
206 if (is_phys_addr(ptr))
207 kfree(ptr);
208 else
209 vfree(ptr);
210}
211
212int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
213 char *secstrings, struct module *mod)
214{
215 unsigned int symindex = 0;
216 unsigned int core_size, init_size;
217 int i;
218
219 mod->arch.phys_plt_offset = 0;
220 mod->arch.virt_plt_offset = 0;
221 mod->arch.phys_plt_tbl = NULL;
222 mod->arch.virt_plt_tbl = NULL;
223
224 if (IS_ENABLED(CONFIG_64BIT))
225 return 0;
226
227 for (i = 1; i < hdr->e_shnum; i++)
228 if (sechdrs[i].sh_type == SHT_SYMTAB)
229 symindex = i;
230
231 core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
232 init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
233
234 if ((core_size + init_size) == 0)
235 return 0;
236
237 mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
238 if (!mod->arch.phys_plt_tbl)
239 return -ENOMEM;
240
241 mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
242 if (!mod->arch.virt_plt_tbl) {
243 __module_free(mod->arch.phys_plt_tbl);
244 mod->arch.phys_plt_tbl = NULL;
245 return -ENOMEM;
246 }
247
248 return 0;
249}
250
251static int apply_r_mips_none(struct module *me, u32 *location,
252 u32 base, Elf_Addr v, bool rela)
253{
254 return 0;
255}
256
257static int apply_r_mips_32(struct module *me, u32 *location,
258 u32 base, Elf_Addr v, bool rela)
259{
260 *location = base + v;
261
262 return 0;
263}
264
265static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
266 void *start, Elf_Addr v)
267{
268 unsigned *tramp = start + *plt_offset;
269 *plt_offset += 4 * sizeof(int);
270
271 /* adjust carry for addiu */
272 if (v & 0x00008000)
273 v += 0x10000;
274
275 tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
276 tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
277 tramp[2] = 0x03200008; /* jr t9 */
278 tramp[3] = 0x00000000; /* nop */
279
280 return (Elf_Addr) tramp;
281}
282
283static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
284{
285 if (is_phys_addr(location))
286 return add_plt_entry_to(&me->arch.phys_plt_offset,
287 me->arch.phys_plt_tbl, v);
288 else
289 return add_plt_entry_to(&me->arch.virt_plt_offset,
290 me->arch.virt_plt_tbl, v);
291
292}
293
294static int apply_r_mips_26(struct module *me, u32 *location,
295 u32 base, Elf_Addr v, bool rela)
296{
297 u32 ofs = base & 0x03ffffff;
298
299 if (v % 4) {
300 pr_err("module %s: dangerous R_MIPS_26 relocation\n",
301 me->name);
302 return -ENOEXEC;
303 }
304
305 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
306 v = add_plt_entry(me, location, v + (ofs << 2));
307 if (!v) {
308 pr_err("module %s: relocation overflow\n",
309 me->name);
310 return -ENOEXEC;
311 }
312 ofs = 0;
313 }
314
315 *location = (*location & ~0x03ffffff) |
316 ((ofs + (v >> 2)) & 0x03ffffff);
317
318 return 0;
319}
320
321static int apply_r_mips_hi16(struct module *me, u32 *location,
322 u32 base, Elf_Addr v, bool rela)
323{
324 struct mips_hi16 *n;
325
326 if (rela) {
327 *location = (*location & 0xffff0000) |
328 ((((long long) v + 0x8000LL) >> 16) & 0xffff);
329 return 0;
330 }
331
332 /*
333 * We cannot relocate this one now because we don't know the value of
334 * the carry we need to add. Save the information, and let LO16 do the
335 * actual relocation.
336 */
337 n = kmalloc(sizeof *n, GFP_KERNEL);
338 if (!n)
339 return -ENOMEM;
340
341 n->addr = (Elf_Addr *)location;
342 n->value = v;
343 n->next = me->arch.r_mips_hi16_list;
344 me->arch.r_mips_hi16_list = n;
345
346 return 0;
347}
348
349static void free_relocation_chain(struct mips_hi16 *l)
350{
351 struct mips_hi16 *next;
352
353 while (l) {
354 next = l->next;
355 kfree(l);
356 l = next;
357 }
358}
359
360static int apply_r_mips_lo16(struct module *me, u32 *location,
361 u32 base, Elf_Addr v, bool rela)
362{
363 unsigned long insnlo = base;
364 struct mips_hi16 *l;
365 Elf_Addr val, vallo;
366
367 if (rela) {
368 *location = (*location & 0xffff0000) | (v & 0xffff);
369 return 0;
370 }
371
372 /* Sign extend the addend we extract from the lo insn. */
373 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
374
375 if (me->arch.r_mips_hi16_list != NULL) {
376 l = me->arch.r_mips_hi16_list;
377 while (l != NULL) {
378 struct mips_hi16 *next;
379 unsigned long insn;
380
381 /*
382 * The value for the HI16 had best be the same.
383 */
384 if (v != l->value)
385 goto out_danger;
386
387 /*
388 * Do the HI16 relocation. Note that we actually don't
389 * need to know anything about the LO16 itself, except
390 * where to find the low 16 bits of the addend needed
391 * by the LO16.
392 */
393 insn = *l->addr;
394 val = ((insn & 0xffff) << 16) + vallo;
395 val += v;
396
397 /*
398 * Account for the sign extension that will happen in
399 * the low bits.
400 */
401 val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
402
403 insn = (insn & ~0xffff) | val;
404 *l->addr = insn;
405
406 next = l->next;
407 kfree(l);
408 l = next;
409 }
410
411 me->arch.r_mips_hi16_list = NULL;
412 }
413
414 /*
415 * Ok, we're done with the HI16 relocs. Now deal with the LO16.
416 */
417 val = v + vallo;
418 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
419 *location = insnlo;
420
421 return 0;
422
423out_danger:
424 free_relocation_chain(l);
425 me->arch.r_mips_hi16_list = NULL;
426
427 pr_err("module %s: dangerous R_MIPS_LO16 relocation\n", me->name);
428
429 return -ENOEXEC;
430}
431
432static int apply_r_mips_pc(struct module *me, u32 *location, u32 base,
433 Elf_Addr v, unsigned int bits)
434{
435 unsigned long mask = GENMASK(bits - 1, 0);
436 unsigned long se_bits;
437 long offset;
438
439 if (v % 4) {
440 pr_err("module %s: dangerous R_MIPS_PC%u relocation\n",
441 me->name, bits);
442 return -ENOEXEC;
443 }
444
445 /* retrieve & sign extend implicit addend if any */
446 offset = base & mask;
447 offset |= (offset & BIT(bits - 1)) ? ~mask : 0;
448
449 offset += ((long)v - (long)location) >> 2;
450
451 /* check the sign bit onwards are identical - ie. we didn't overflow */
452 se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0;
453 if ((offset & ~mask) != (se_bits & ~mask)) {
454 pr_err("module %s: relocation overflow\n", me->name);
455 return -ENOEXEC;
456 }
457
458 *location = (*location & ~mask) | (offset & mask);
459
460 return 0;
461}
462
463static int apply_r_mips_pc16(struct module *me, u32 *location,
464 u32 base, Elf_Addr v, bool rela)
465{
466 return apply_r_mips_pc(me, location, base, v, 16);
467}
468
469static int apply_r_mips_pc21(struct module *me, u32 *location,
470 u32 base, Elf_Addr v, bool rela)
471{
472 return apply_r_mips_pc(me, location, base, v, 21);
473}
474
475static int apply_r_mips_pc26(struct module *me, u32 *location,
476 u32 base, Elf_Addr v, bool rela)
477{
478 return apply_r_mips_pc(me, location, base, v, 26);
479}
480
481static int apply_r_mips_64(struct module *me, u32 *location,
482 u32 base, Elf_Addr v, bool rela)
483{
484 if (WARN_ON(!rela))
485 return -EINVAL;
486
487 *(Elf_Addr *)location = v;
488
489 return 0;
490}
491
492static int apply_r_mips_higher(struct module *me, u32 *location,
493 u32 base, Elf_Addr v, bool rela)
494{
495 if (WARN_ON(!rela))
496 return -EINVAL;
497
498 *location = (*location & 0xffff0000) |
499 ((((long long)v + 0x80008000LL) >> 32) & 0xffff);
500
501 return 0;
502}
503
504static int apply_r_mips_highest(struct module *me, u32 *location,
505 u32 base, Elf_Addr v, bool rela)
506{
507 if (WARN_ON(!rela))
508 return -EINVAL;
509
510 *location = (*location & 0xffff0000) |
511 ((((long long)v + 0x800080008000LL) >> 48) & 0xffff);
512
513 return 0;
514}
515
516/**
517 * reloc_handler() - Apply a particular relocation to a module
518 * @me: the module to apply the reloc to
519 * @location: the address at which the reloc is to be applied
520 * @base: the existing value at location for REL-style; 0 for RELA-style
521 * @v: the value of the reloc, with addend for RELA-style
522 *
523 * Each implemented reloc_handler function applies a particular type of
524 * relocation to the module @me. Relocs that may be found in either REL or RELA
525 * variants can be handled by making use of the @base & @v parameters which are
526 * set to values which abstract the difference away from the particular reloc
527 * implementations.
528 *
529 * Return: 0 upon success, else -ERRNO
530 */
531typedef int (*reloc_handler)(struct module *me, u32 *location,
532 u32 base, Elf_Addr v, bool rela);
533
534/* The handlers for known reloc types */
535static reloc_handler reloc_handlers[] = {
536 [R_MIPS_NONE] = apply_r_mips_none,
537 [R_MIPS_32] = apply_r_mips_32,
538 [R_MIPS_26] = apply_r_mips_26,
539 [R_MIPS_HI16] = apply_r_mips_hi16,
540 [R_MIPS_LO16] = apply_r_mips_lo16,
541 [R_MIPS_PC16] = apply_r_mips_pc16,
542 [R_MIPS_64] = apply_r_mips_64,
543 [R_MIPS_HIGHER] = apply_r_mips_higher,
544 [R_MIPS_HIGHEST] = apply_r_mips_highest,
545 [R_MIPS_PC21_S2] = apply_r_mips_pc21,
546 [R_MIPS_PC26_S2] = apply_r_mips_pc26,
547};
548
549static int __apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
550 unsigned int symindex, unsigned int relsec,
551 struct module *me, bool rela)
552{
553 union {
554 Elf_Mips_Rel *rel;
555 Elf_Mips_Rela *rela;
556 } r;
557 reloc_handler handler;
558 Elf_Sym *sym;
559 u32 *location, base;
560 unsigned int i, type;
561 Elf_Addr v;
562 int err = 0;
563 size_t reloc_sz;
564
565 pr_debug("Applying relocate section %u to %u\n", relsec,
566 sechdrs[relsec].sh_info);
567
568 r.rel = (void *)sechdrs[relsec].sh_addr;
569 reloc_sz = rela ? sizeof(*r.rela) : sizeof(*r.rel);
570 me->arch.r_mips_hi16_list = NULL;
571 for (i = 0; i < sechdrs[relsec].sh_size / reloc_sz; i++) {
572 /* This is where to make the change */
573 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
574 + r.rel->r_offset;
575 /* This is the symbol it is referring to */
576 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
577 + ELF_MIPS_R_SYM(*r.rel);
578 if (sym->st_value >= -MAX_ERRNO) {
579 /* Ignore unresolved weak symbol */
580 if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
581 continue;
582 pr_warn("%s: Unknown symbol %s\n",
583 me->name, strtab + sym->st_name);
584 err = -ENOENT;
585 goto out;
586 }
587
588 type = ELF_MIPS_R_TYPE(*r.rel);
589 if (type < ARRAY_SIZE(reloc_handlers))
590 handler = reloc_handlers[type];
591 else
592 handler = NULL;
593
594 if (!handler) {
595 pr_err("%s: Unknown relocation type %u\n",
596 me->name, type);
597 err = -EINVAL;
598 goto out;
599 }
600
601 if (rela) {
602 v = sym->st_value + r.rela->r_addend;
603 base = 0;
604 r.rela = &r.rela[1];
605 } else {
606 v = sym->st_value;
607 base = *location;
608 r.rel = &r.rel[1];
609 }
610
611 err = handler(me, location, base, v, rela);
612 if (err)
613 goto out;
614 }
615
616out:
617 /*
618 * Normally the hi16 list should be deallocated at this point. A
619 * malformed binary however could contain a series of R_MIPS_HI16
620 * relocations not followed by a R_MIPS_LO16 relocation, or if we hit
621 * an error processing a reloc we might have gotten here before
622 * reaching the R_MIPS_LO16. In either case, free up the list and
623 * return an error.
624 */
625 if (me->arch.r_mips_hi16_list) {
626 free_relocation_chain(me->arch.r_mips_hi16_list);
627 me->arch.r_mips_hi16_list = NULL;
628 err = err ?: -ENOEXEC;
629 }
630
631 return err;
632}
633
634int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
635 unsigned int symindex, unsigned int relsec,
636 struct module *me)
637{
638 return __apply_relocate(sechdrs, strtab, symindex, relsec, me, false);
639}
640
641#ifdef CONFIG_MODULES_USE_ELF_RELA
642int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
643 unsigned int symindex, unsigned int relsec,
644 struct module *me)
645{
646 return __apply_relocate(sechdrs, strtab, symindex, relsec, me, true);
647}
648#endif /* CONFIG_MODULES_USE_ELF_RELA */
649
650/* Given an address, look for it in the module exception tables. */
651const struct exception_table_entry *search_module_dbetables(unsigned long addr)
652{
653 unsigned long flags;
654 const struct exception_table_entry *e = NULL;
655 struct mod_arch_specific *dbe;
656
657 spin_lock_irqsave(&dbe_lock, flags);
658 list_for_each_entry(dbe, &dbe_list, dbe_list) {
659 e = search_extable(dbe->dbe_start,
660 dbe->dbe_end - dbe->dbe_start, addr);
661 if (e)
662 break;
663 }
664 spin_unlock_irqrestore(&dbe_lock, flags);
665
666 /* Now, if we found one, we are running inside it now, hence
667 we cannot unload the module, hence no refcnt needed. */
668 return e;
669}
670
671/* Put in dbe list if necessary. */
672int module_finalize(const Elf_Ehdr *hdr,
673 const Elf_Shdr *sechdrs,
674 struct module *me)
675{
676 const Elf_Shdr *s;
677 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
678
679 /* Make jump label nops. */
680 jump_label_apply_nops(me);
681
682 INIT_LIST_HEAD(&me->arch.dbe_list);
683 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
684 if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
685 continue;
686 me->arch.dbe_start = (void *)s->sh_addr;
687 me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
688 spin_lock_irq(&dbe_lock);
689 list_add(&me->arch.dbe_list, &dbe_list);
690 spin_unlock_irq(&dbe_lock);
691 }
692
693 /* Get rid of the fixup trampoline if we're running the module
694 * from physically mapped address space */
695 if (me->arch.phys_plt_offset == 0) {
696 __module_free(me->arch.phys_plt_tbl);
697 me->arch.phys_plt_tbl = NULL;
698 }
699 if (me->arch.virt_plt_offset == 0) {
700 __module_free(me->arch.virt_plt_tbl);
701 me->arch.virt_plt_tbl = NULL;
702 }
703
704 return 0;
705}
706
707void module_arch_freeing_init(struct module *mod)
708{
709 if (mod->state == MODULE_STATE_LIVE)
710 return;
711
712 if (mod->arch.phys_plt_tbl) {
713 __module_free(mod->arch.phys_plt_tbl);
714 mod->arch.phys_plt_tbl = NULL;
715 }
716 if (mod->arch.virt_plt_tbl) {
717 __module_free(mod->arch.virt_plt_tbl);
718 mod->arch.virt_plt_tbl = NULL;
719 }
720}
721
722void module_arch_cleanup(struct module *mod)
723{
724 spin_lock_irq(&dbe_lock);
725 list_del(&mod->arch.dbe_list);
726 spin_unlock_irq(&dbe_lock);
727}