1 --- a/arch/mips/Makefile
2 +++ b/arch/mips/Makefile
3 @@ -90,8 +90,18 @@ all-$(CONFIG_SYS_SUPPORTS_ZBOOT)+= vmlin
4 cflags-y += -G 0 -mno-abicalls -fno-pic -pipe -mno-branch-likely
5 cflags-y += -msoft-float
6 LDFLAGS_vmlinux += -G 0 -static -n -nostdlib --gc-sections
8 KBUILD_AFLAGS_MODULE += -mlong-calls
9 KBUILD_CFLAGS_MODULE += -mlong-calls
11 + ifdef CONFIG_DYNAMIC_FTRACE
12 + KBUILD_AFLAGS_MODULE += -mlong-calls
13 + KBUILD_CFLAGS_MODULE += -mlong-calls
15 + KBUILD_AFLAGS_MODULE += -mno-long-calls
16 + KBUILD_CFLAGS_MODULE += -mno-long-calls
20 ifndef CONFIG_FUNCTION_TRACER
21 KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
22 --- a/arch/mips/include/asm/module.h
23 +++ b/arch/mips/include/asm/module.h
24 @@ -11,6 +11,11 @@ struct mod_arch_specific {
25 const struct exception_table_entry *dbe_start;
26 const struct exception_table_entry *dbe_end;
27 struct mips_hi16 *r_mips_hi16_list;
31 + unsigned int phys_plt_offset;
32 + unsigned int virt_plt_offset;
35 typedef uint8_t Elf64_Byte; /* Type for a 8-bit quantity. */
36 --- a/arch/mips/kernel/module.c
37 +++ b/arch/mips/kernel/module.c
38 @@ -43,14 +43,222 @@ struct mips_hi16 {
39 static LIST_HEAD(dbe_list);
40 static DEFINE_SPINLOCK(dbe_lock);
44 + * Get the potential max trampolines size required of the init and
45 + * non-init sections. Only used if we cannot find enough contiguous
46 + * physically mapped memory to put the module into.
49 +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
50 + const char *secstrings, unsigned int symindex, bool is_init)
52 + unsigned long ret = 0;
56 + /* Everything marked ALLOC (this includes the exported symbols) */
57 + for (i = 1; i < hdr->e_shnum; ++i) {
58 + unsigned int info = sechdrs[i].sh_info;
60 + if (sechdrs[i].sh_type != SHT_REL
61 + && sechdrs[i].sh_type != SHT_RELA)
64 + /* Not a valid relocation section? */
65 + if (info >= hdr->e_shnum)
68 + /* Don't bother with non-allocated sections */
69 + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
72 + /* If it's called *.init*, and we're not init, we're
74 + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
78 + syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
79 + if (sechdrs[i].sh_type == SHT_REL) {
80 + Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
81 + unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
83 + for (j = 0; j < size; ++j) {
86 + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
89 + sym = syms + ELF_MIPS_R_SYM(rel[j]);
90 + if (!is_init && sym->st_shndx != SHN_UNDEF)
93 + ret += 4 * sizeof(int);
96 + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
97 + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
99 + for (j = 0; j < size; ++j) {
102 + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
105 + sym = syms + ELF_MIPS_R_SYM(rela[j]);
106 + if (!is_init && sym->st_shndx != SHN_UNDEF)
109 + ret += 4 * sizeof(int);
117 +#ifndef MODULE_START
118 +static void *alloc_phys(unsigned long size)
124 + size = PAGE_ALIGN(size);
125 + order = get_order(size);
127 + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
128 + __GFP_THISNODE, order);
132 + split_page(page, order);
134 + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
137 + return page_address(page);
141 +static void free_phys(void *ptr, unsigned long size)
146 + page = virt_to_page(ptr);
147 + end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
149 + for (; page < end; ++page)
154 void *module_alloc(unsigned long size)
157 return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
158 GFP_KERNEL, PAGE_KERNEL, NUMA_NO_NODE,
159 __builtin_return_address(0));
166 + ptr = alloc_phys(size);
168 + /* If we failed to allocate physically contiguous memory,
169 + * fall back to regular vmalloc. The module loader code will
170 + * create jump tables to handle long jumps */
172 + return vmalloc(size);
178 +static inline bool is_phys_addr(void *ptr)
181 + return (KSEGX((unsigned long)ptr) == CKSEG0);
183 + return (KSEGX(ptr) == KSEG0);
187 +/* Free memory returned from module_alloc */
188 +void module_free(struct module *mod, void *module_region)
190 + if (is_phys_addr(module_region)) {
191 + if (mod->module_init == module_region)
192 + free_phys(module_region, mod->init_size);
193 + else if (mod->module_core == module_region)
194 + free_phys(module_region, mod->core_size);
198 + vfree(module_region);
202 +static void *__module_alloc(int size, bool phys)
207 + ptr = kmalloc(size, GFP_KERNEL);
209 + ptr = vmalloc(size);
213 +static void __module_free(void *ptr)
215 + if (is_phys_addr(ptr))
221 +int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
222 + char *secstrings, struct module *mod)
224 + unsigned int symindex = 0;
225 + unsigned int core_size, init_size;
228 + mod->arch.phys_plt_offset = 0;
229 + mod->arch.virt_plt_offset = 0;
230 + mod->arch.phys_plt_tbl = NULL;
231 + mod->arch.virt_plt_tbl = NULL;
233 + if (IS_ENABLED(CONFIG_64BIT))
236 + for (i = 1; i < hdr->e_shnum; i++)
237 + if (sechdrs[i].sh_type == SHT_SYMTAB)
240 + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
241 + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
243 + if ((core_size + init_size) == 0)
246 + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
247 + if (!mod->arch.phys_plt_tbl)
250 + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
251 + if (!mod->arch.virt_plt_tbl) {
252 + __module_free(mod->arch.phys_plt_tbl);
253 + mod->arch.phys_plt_tbl = NULL;
260 int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
262 @@ -64,8 +272,39 @@ static int apply_r_mips_32_rel(struct mo
266 +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
267 + void *start, Elf_Addr v)
269 + unsigned *tramp = start + *plt_offset;
270 + *plt_offset += 4 * sizeof(int);
272 + /* adjust carry for addiu */
273 + if (v & 0x00008000)
276 + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
277 + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
278 + tramp[2] = 0x03200008; /* jr t9 */
279 + tramp[3] = 0x00000000; /* nop */
281 + return (Elf_Addr) tramp;
284 +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
286 + if (is_phys_addr(location))
287 + return add_plt_entry_to(&me->arch.phys_plt_offset,
288 + me->arch.phys_plt_tbl, v);
290 + return add_plt_entry_to(&me->arch.virt_plt_offset,
291 + me->arch.virt_plt_tbl, v);
295 static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
297 + u32 ofs = *location & 0x03ffffff;
300 pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
302 @@ -73,14 +312,17 @@ static int apply_r_mips_26_rel(struct mo
305 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
307 - "module %s: relocation overflow\n",
310 + v = add_plt_entry(me, location, v + (ofs << 2));
313 + "module %s: relocation overflow\n", me->name);
319 *location = (*location & ~0x03ffffff) |
320 - ((*location + (v >> 2)) & 0x03ffffff);
321 + ((ofs + (v >> 2)) & 0x03ffffff);
325 @@ -287,9 +529,36 @@ int module_finalize(const Elf_Ehdr *hdr,
326 list_add(&me->arch.dbe_list, &dbe_list);
327 spin_unlock_irq(&dbe_lock);
330 + /* Get rid of the fixup trampoline if we're running the module
331 + * from physically mapped address space */
332 + if (me->arch.phys_plt_offset == 0) {
333 + __module_free(me->arch.phys_plt_tbl);
334 + me->arch.phys_plt_tbl = NULL;
336 + if (me->arch.virt_plt_offset == 0) {
337 + __module_free(me->arch.virt_plt_tbl);
338 + me->arch.virt_plt_tbl = NULL;
344 +void module_arch_freeing_init(struct module *mod)
346 + if (mod->state == MODULE_STATE_LIVE)
349 + if (mod->arch.phys_plt_tbl) {
350 + __module_free(mod->arch.phys_plt_tbl);
351 + mod->arch.phys_plt_tbl = NULL;
353 + if (mod->arch.virt_plt_tbl) {
354 + __module_free(mod->arch.virt_plt_tbl);
355 + mod->arch.virt_plt_tbl = NULL;
359 void module_arch_cleanup(struct module *mod)
361 spin_lock_irq(&dbe_lock);