1 --- a/arch/mips/Makefile
2 +++ b/arch/mips/Makefile
3 @@ -90,8 +90,13 @@ 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 +KBUILD_AFLAGS_MODULE += -mno-long-calls
12 +KBUILD_CFLAGS_MODULE += -mno-long-calls
15 ifndef CONFIG_FUNCTION_TRACER
16 KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
17 --- a/arch/mips/include/asm/module.h
18 +++ b/arch/mips/include/asm/module.h
19 @@ -11,6 +11,11 @@ struct mod_arch_specific {
20 const struct exception_table_entry *dbe_start;
21 const struct exception_table_entry *dbe_end;
22 struct mips_hi16 *r_mips_hi16_list;
26 + unsigned int phys_plt_offset;
27 + unsigned int virt_plt_offset;
30 typedef uint8_t Elf64_Byte; /* Type for a 8-bit quantity. */
31 --- a/arch/mips/kernel/module.c
32 +++ b/arch/mips/kernel/module.c
33 @@ -43,14 +43,222 @@ struct mips_hi16 {
34 static LIST_HEAD(dbe_list);
35 static DEFINE_SPINLOCK(dbe_lock);
39 + * Get the potential max trampolines size required of the init and
40 + * non-init sections. Only used if we cannot find enough contiguous
41 + * physically mapped memory to put the module into.
44 +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
45 + const char *secstrings, unsigned int symindex, bool is_init)
47 + unsigned long ret = 0;
51 + /* Everything marked ALLOC (this includes the exported symbols) */
52 + for (i = 1; i < hdr->e_shnum; ++i) {
53 + unsigned int info = sechdrs[i].sh_info;
55 + if (sechdrs[i].sh_type != SHT_REL
56 + && sechdrs[i].sh_type != SHT_RELA)
59 + /* Not a valid relocation section? */
60 + if (info >= hdr->e_shnum)
63 + /* Don't bother with non-allocated sections */
64 + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
67 + /* If it's called *.init*, and we're not init, we're
69 + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
73 + syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
74 + if (sechdrs[i].sh_type == SHT_REL) {
75 + Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
76 + unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
78 + for (j = 0; j < size; ++j) {
81 + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
84 + sym = syms + ELF_MIPS_R_SYM(rel[j]);
85 + if (!is_init && sym->st_shndx != SHN_UNDEF)
88 + ret += 4 * sizeof(int);
91 + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
92 + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
94 + for (j = 0; j < size; ++j) {
97 + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
100 + sym = syms + ELF_MIPS_R_SYM(rela[j]);
101 + if (!is_init && sym->st_shndx != SHN_UNDEF)
104 + ret += 4 * sizeof(int);
112 +#ifndef MODULE_START
113 +static void *alloc_phys(unsigned long size)
119 + size = PAGE_ALIGN(size);
120 + order = get_order(size);
122 + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
123 + __GFP_THISNODE, order);
127 + split_page(page, order);
129 + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
132 + return page_address(page);
136 +static void free_phys(void *ptr, unsigned long size)
141 + page = virt_to_page(ptr);
142 + end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
144 + for (; page < end; ++page)
149 void *module_alloc(unsigned long size)
152 return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
153 GFP_KERNEL, PAGE_KERNEL, NUMA_NO_NODE,
154 __builtin_return_address(0));
161 + ptr = alloc_phys(size);
163 + /* If we failed to allocate physically contiguous memory,
164 + * fall back to regular vmalloc. The module loader code will
165 + * create jump tables to handle long jumps */
167 + return vmalloc(size);
173 +static inline bool is_phys_addr(void *ptr)
176 + return (KSEGX((unsigned long)ptr) == CKSEG0);
178 + return (KSEGX(ptr) == KSEG0);
182 +/* Free memory returned from module_alloc */
183 +void module_free(struct module *mod, void *module_region)
185 + if (is_phys_addr(module_region)) {
186 + if (mod->module_init == module_region)
187 + free_phys(module_region, mod->init_size);
188 + else if (mod->module_core == module_region)
189 + free_phys(module_region, mod->core_size);
193 + vfree(module_region);
197 +static void *__module_alloc(int size, bool phys)
202 + ptr = kmalloc(size, GFP_KERNEL);
204 + ptr = vmalloc(size);
208 +static void __module_free(void *ptr)
210 + if (is_phys_addr(ptr))
216 +int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
217 + char *secstrings, struct module *mod)
219 + unsigned int symindex = 0;
220 + unsigned int core_size, init_size;
223 + mod->arch.phys_plt_offset = 0;
224 + mod->arch.virt_plt_offset = 0;
225 + mod->arch.phys_plt_tbl = NULL;
226 + mod->arch.virt_plt_tbl = NULL;
228 + if (IS_ENABLED(CONFIG_64BIT))
231 + for (i = 1; i < hdr->e_shnum; i++)
232 + if (sechdrs[i].sh_type == SHT_SYMTAB)
235 + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
236 + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
238 + if ((core_size + init_size) == 0)
241 + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
242 + if (!mod->arch.phys_plt_tbl)
245 + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
246 + if (!mod->arch.virt_plt_tbl) {
247 + __module_free(mod->arch.phys_plt_tbl);
248 + mod->arch.phys_plt_tbl = NULL;
255 int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
257 @@ -64,8 +272,39 @@ static int apply_r_mips_32_rel(struct mo
261 +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
262 + void *start, Elf_Addr v)
264 + unsigned *tramp = start + *plt_offset;
265 + *plt_offset += 4 * sizeof(int);
267 + /* adjust carry for addiu */
268 + if (v & 0x00008000)
271 + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
272 + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
273 + tramp[2] = 0x03200008; /* jr t9 */
274 + tramp[3] = 0x00000000; /* nop */
276 + return (Elf_Addr) tramp;
279 +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
281 + if (is_phys_addr(location))
282 + return add_plt_entry_to(&me->arch.phys_plt_offset,
283 + me->arch.phys_plt_tbl, v);
285 + return add_plt_entry_to(&me->arch.virt_plt_offset,
286 + me->arch.virt_plt_tbl, v);
290 static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
292 + u32 ofs = *location & 0x03ffffff;
295 pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
297 @@ -73,14 +312,17 @@ static int apply_r_mips_26_rel(struct mo
300 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
302 - "module %s: relocation overflow\n",
305 + v = add_plt_entry(me, location, v + (ofs << 2));
308 + "module %s: relocation overflow\n", me->name);
314 *location = (*location & ~0x03ffffff) |
315 - ((*location + (v >> 2)) & 0x03ffffff);
316 + ((ofs + (v >> 2)) & 0x03ffffff);
320 @@ -287,9 +529,36 @@ int module_finalize(const Elf_Ehdr *hdr,
321 list_add(&me->arch.dbe_list, &dbe_list);
322 spin_unlock_irq(&dbe_lock);
325 + /* Get rid of the fixup trampoline if we're running the module
326 + * from physically mapped address space */
327 + if (me->arch.phys_plt_offset == 0) {
328 + __module_free(me->arch.phys_plt_tbl);
329 + me->arch.phys_plt_tbl = NULL;
331 + if (me->arch.virt_plt_offset == 0) {
332 + __module_free(me->arch.virt_plt_tbl);
333 + me->arch.virt_plt_tbl = NULL;
339 +void module_arch_freeing_init(struct module *mod)
341 + if (mod->state == MODULE_STATE_LIVE)
344 + if (mod->arch.phys_plt_tbl) {
345 + __module_free(mod->arch.phys_plt_tbl);
346 + mod->arch.phys_plt_tbl = NULL;
348 + if (mod->arch.virt_plt_tbl) {
349 + __module_free(mod->arch.virt_plt_tbl);
350 + mod->arch.virt_plt_tbl = NULL;
354 void module_arch_cleanup(struct module *mod)
356 spin_lock_irq(&dbe_lock);